package com.xt.mvcpilot.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.xt.mvcpilot.data.User;


/**
 * User validator
 * Not used anymore, replaced by Annotations direct in User class!
 * 
 * @author txiao
 *
 */
public class UserValidator implements Validator{
	
	public boolean supports(Class clazz) {
	   //just validate the User instances
	   return User.class.isAssignableFrom(clazz);
	}

	public void validate(Object target, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "user.validator.username.required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "user.validator.firstname.required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "surname", "user.validator.surname.required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "user.validator.email.required");
	}
	
	
	public void validateCreate(Object target, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "user.validator.username.required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "user.validator.password.required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "user.validator.firstname.required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "surname", "user.validator.surname.required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "user.validator.email.required");
		
		//errors.reject("", arg1)
	}
}
